package com.example.shop_2210.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.LoginEntityResult
import com.example.base.entity.PasswordEntityResult
import com.example.base.entity.SendCodeEntityResult
import com.example.base.mvvm.BaseViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

class LoginViewModel:BaseViewModel() {
    //验证码登录
    val loginCodeSuccess = MutableLiveData<LoginEntityResult>()
    val loginCodeFail = MutableLiveData<String>()
    fun loginCode(captcha:String,phone:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map.put("captcha",captcha)
                map.put("phone",phone)
                map.put("spread_spid",0)
                val json = Gson().toJson(map)
                val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(),json)
                loginCodeSuccess.value = apiService.loginCode(body)

            }catch (e:Exception){
                //异常情况---例如断网了，给的实体类不正确，没办法转换
                //loginCodeFail.postValue(e.message)
                loginCodeFail.value = e.message
            }
        }
    }

    //获取验证码
    val sendCodeSuccess = MutableLiveData<SendCodeEntityResult>()
    val sendCodeFail = MutableLiveData<String>()
    fun sendCode(phone:String){
        viewModelScope.launch {
            try {
                sendCodeSuccess.value = apiService.sendCode(phone)
            }catch (e:Exception){
                sendCodeFail.value = e.message
            }
        }
    }

    //修改密码
    val passwordSuccess = MutableLiveData<PasswordEntityResult>()
    val passwordFail = MutableLiveData<String>()
    fun password(account:String,captcha:String,password:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map.put("account",account)
                map.put("captcha",captcha)
                map.put("password",password)
                val json = Gson().toJson(map)
                val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(),json)
                passwordSuccess.value = apiService.password(body)
            }catch (e:Exception){
                passwordFail.value = e.message
            }
        }
    }

    //用户名密码登录
    val loginSuccess = MutableLiveData<LoginEntityResult>()
    val loginFail = MutableLiveData<String>()
    fun login(account:String,password:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map.put("account",account)
                map.put("password",password)
                map.put("spread_spid",0)
                val json = Gson().toJson(map)
                val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(),json)
                loginSuccess.value = apiService.login(body)
            }catch (e:Exception){
                loginFail.value = e.message
            }
        }
    }

}